<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
p, li { white-space: pre-wrap; }
</style></head><body style=" font-family:'DejaVu Sans'; font-size:11pt; font-weight:400; font-style:normal;">
<html>
<header class="entry-header"><h1>None (null), или немного о типе NoneType</h1></header><div id="ytb" style="padding-left:3%"><div id="yandex_rtb_R-A-139987-4"><yatag class="q398cc43f k39d92d8d" id="q398cc43f" lang="ru"><yatag class="p6c0eb6f1 m39e5d83c u2d7e984d w211fdb59 h191a5682"><yatag class="ve5b506cd i482e5942"><yatag class="vd7857ad3"><table class="x81f2a2af p28eeca14 a92f38f9e"><tbody><tr class="je442d6fb tb78b4da k519db3ee"><td class="g73b7fb7 m42ecc76 te4c348c2 k3e8c32b2"><yatag data-id="4257598804" class="qb85aa20 t99730038 l764cd6fd u1dc12e2 l5c009abe y9ca54437"><yatag class="v80fb866c"><a target="_blank" class="bc39cf8c" href="https://an.yandex.ru/count/6a6gmsiHl9a50Eu1CKXPQri00000EAQB4K02I09Wl0Xe172exUw02e01m9oQWBowxEVj0OW1bgAFy32G0QYrhjmVc06adSxQ8A01fhMkt1-e0PATpjeWk07IeRZ85C010jW1tAIE3-01jecD3kW1dW7u0G680WIW0ixKgHQv0fKmhek7oiWAy0AUokV02VW2We20W830Qe03sRtyiWk80ulNfFy7c0FqWWEm0mIu1Fy1w0I95_W4Zv8KY0MFaXIG1VM06A05k8K6g0MwZmMm1RgF1RW5e8q6m0NrW1Z81Q2D1j05gxS2u0Lby0K1c0Q0qApp3g06vAW1g0Rag06u1WR91heeCTXEN_K0qGQ6wv8Mcua66Ta60000K2e0002G1pQf1rSXcCWNYIC_i0U0W90ym0Vko8At2j070k07XWg020RG2BgAW870a802u0YdwfGBW0e1mGe00000003mFzWA0k0AW8bw-0h0_1M82mYg2n0GbU5_Lo400FA75CFoSWK0m0k0emN82u3Kam7P2rSXcCWNYIC_w0kFaXJm2mg83ERtthu1w0m6c0ssvbpo3G3w3G223W293W0000000F0_a0x0X3sO3hhHnC7jwTTwe0x0X3sm3W6X3m0000000F0_g0_Nml2ysQBYaJy0?stat-id=4&amp;test-tag=468396516835329&amp;format-type=0&amp;banner-test-tags=eyI0MjU3NTk4ODA0IjoiNDY4MzkxOTUzNDY1MzQ0In0%3D&amp;" style="background-image: url(&quot;avatars.mds.yandex.net/get-direct/403384/exaimEdWnPAgcCbuUFgVrA/x450&quot;) !important; max-width: 338px !important;"></a><yatag class="u5825783c te1459fce" style="font-size: 16.0592px !important;"><yatag class="g74844b62" style="height: 166.95px !important;"><yatag class="g70d3e44b"><a target="_blank" class="r12a5a1c6" href="https://an.yandex.ru/count/6a6gmsiHl9a50Eu1CKXPQri00000EAQB4K02I09Wl0Xe172exUw02e01m9oQWBowxEVj0OW1bgAFy32G0QYrhjmVc06adSxQ8A01fhMkt1-e0PATpjeWk07IeRZ85C010jW1tAIE3-01jecD3kW1dW7u0G680WIW0ixKgHQv0fKmhek7oiWAy0AUokV02VW2We20W830Qe03sRtyiWk80ulNfFy7c0FqWWEm0mIu1Fy1w0I95_W4Zv8KY0MFaXIG1VM06A05k8K6g0MwZmMm1RgF1RW5e8q6m0NrW1Z81Q2D1j05gxS2u0Lby0K1c0Q0qApp3g06vAW1g0Rag06u1WR91heeCTXEN_K0qGQ6wv8Mcua66Ta60000K2e0002G1pQf1rSXcCWNYIC_i0U0W90ym0Vko8At2j070k07XWg020RG2BgAW870a802u0YdwfGBW0e1mGe00000003mFzWA0k0AW8bw-0h0_1M82mYg2n0GbU5_Lo400FA75CFoSWK0m0k0emN82u3Kam7P2rSXcCWNYIC_w0kFaXJm2mg83ERtthu1w0m6c0ssvbpo3G3w3G223W293W0000000F0_a0x0X3sO3hhHnC7jwTTwe0x0X3sm3W6X3m0000000F0_g0_Nml2ysQBYaJy0?stat-id=4&amp;test-tag=468396516835329&amp;format-type=0&amp;banner-test-tags=eyI0MjU3NTk4ODA0IjoiNDY4MzkxOTUzNDY1MzQ0In0%3D&amp;"><yatag class="ge9745d90 ba42669fe">Курс программирования на Python</yatag></a></yatag><yatag class="r2fb9bf6d"><yatag class="aeb174a76"><a target="_blank" class="a5752f05b" href="https://an.yandex.ru/count/6a6gmsiHl9a50Eu1CKXPQri00000EAQB4K02I09Wl0Xe172exUw02e01m9oQWBowxEVj0OW1bgAFy32G0QYrhjmVc06adSxQ8A01fhMkt1-e0PATpjeWk07IeRZ85C010jW1tAIE3-01jecD3kW1dW7u0G680WIW0ixKgHQv0fKmhek7oiWAy0AUokV02VW2We20W830Qe03sRtyiWk80ulNfFy7c0FqWWEm0mIu1Fy1w0I95_W4Zv8KY0MFaXIG1VM06A05k8K6g0MwZmMm1RgF1RW5e8q6m0NrW1Z81Q2D1j05gxS2u0Lby0K1c0Q0qApp3g06vAW1g0Rag06u1WR91heeCTXEN_K0qGQ6wv8Mcua66Ta60000K2e0002G1pQf1rSXcCWNYIC_i0U0W90ym0Vko8At2j070k07XWg020RG2BgAW870a802u0YdwfGBW0e1mGe00000003mFzWA0k0AW8bw-0h0_1M82mYg2n0GbU5_Lo400FA75CFoSWK0m0k0emN82u3Kam7P2rSXcCWNYIC_w0kFaXJm2mg83ERtthu1w0m6c0ssvbpo3G3w3G223W293W0000000F0_a0x0X3sO3hhHnC7jwTTwe0x0X3sm3W6X3m0000000F0_g0_Nml2ysQBYaJy0?stat-id=4&amp;test-tag=468396516835329&amp;format-type=0&amp;banner-test-tags=eyI0MjU3NTk4ODA0IjoiNDY4MzkxOTUzNDY1MzQ0In0%3D&amp;"><yatag class="ge9745d90 ba42669fe">Продвинутый уровень, авторская программа. Спец условия трудоустройства выпускникам.</yatag></a></yatag><yatag class="ib25052d fd71f2679"><yatag class="waddb1d79"><yatag class="u85bd0f3"><a target="_blank" class="s15a223be" href="https://an.yandex.ru/count/6a6gmr2E8P450Eu1CKXPQri00000EAQB4K02I09Wl0Xe172exUw02e01m9oQWBowxEVj0OW1bgAFy32G0QYrhjmVc06adSxQ8A01fhMkt1-e0PATpjeWk07IeRZ85C01FjW1tAIE3-01jecD3kW1dW7u0G680WIW0ixKgHQv0fKmhek7oiWAy0AUokV02VW2We20W830Qe03sRtyiWk80ulNfFy7c0FqWWEm0mIu1Fy1w0I95_W4Zv8KY0MFaXIG1VM06A05k8K6g0MwZmMm1RgF1RW5e8q6m0NrW1Z81Q2D1j05gxS2u0Lby0K1c0Q0qApp3g06vAW1g0Rag06u1WR91heeCTXEN_K0qGQ6wv8Mcua66Ta60000K2e0002G1pQf1rSXcCWNYIC_i0U0W90ym0Vko8At2j070k07XWg020RG2BgAW870a802u0YdwfGBW0e1mGe00000003mFzWA0k0AW8bw-0h0_1M82mYg2n0GbU5_Lo400FA75CFoSWK0m0k0emN82u3Kam7P2rSXcCWNYIC_w0kFaXJm2mg83ERtthu1w0m6c0ssvbpo3G3w3G223W293W0000000F0_a0x0X3sO3hhHnC7jwTTwe0x0X3sm3W6X3m0000000F0_g0_Nml2ysQBYaJy0?stat-id=4&amp;test-tag=468396516835329&amp;format-type=0&amp;banner-test-tags=eyI0MjU3NTk4ODA0IjoiNDY4MzkxOTUzNDY1MzQ0In0%3D&amp;"><yatag class="ge9745d90 ba42669fe">О проекте</yatag></a><a target="_blank" class="s15a223be" href="https://an.yandex.ru/count/6a6gmmUZ0Ee50Eu1CKXPQri00000EAQB4K02I09Wl0Xe172exUw02e01m9oQWBowxEVj0OW1bgAFy32G0QYrhjmVc06adSxQ8A01fhMkt1-e0PATpjeWk07IeRZ85C01GjW1tAIE3-01jecD3kW1dW7u0G680WIW0ixKgHQv0fKmhek7oiWAy0AUokV02VW2We20W830Qe03sRtyiWk80ulNfFy7c0FqWWEm0mIu1Fy1w0I95_W4Zv8KY0MFaXIG1VM06A05k8K6g0MwZmMm1RgF1RW5e8q6m0NrW1Z81Q2D1j05gxS2u0Lby0K1c0Q0qApp3g06vAW1g0Rag06u1WR91heeCTXEN_K0qGQ6wv8Mcua66Ta60000K2e0002G1pQf1rSXcCWNYIC_i0U0W90ym0Vko8At2j070k07XWg020RG2BgAW870a802u0YdwfGBW0e1mGe00000003mFzWA0k0AW8bw-0h0_1M82mYg2n0GbU5_Lo400FA75CFoSWK0m0k0emN82u3Kam7P2rSXcCWNYIC_w0kFaXJm2mg83ERtthu1w0m6c0ssvbpo3G3w3G223W293W0000000F0_a0x0X3sO3hhHnC7jwTTwe0x0X3sm3W6X3m0000000F0_g0_Nml2ysQBYaJy0?stat-id=4&amp;test-tag=468396516835329&amp;format-type=0&amp;banner-test-tags=eyI0MjU3NTk4ODA0IjoiNDY4MzkxOTUzNDY1MzQ0In0%3D&amp;"><yatag class="ge9745d90 ba42669fe">Программа курса</yatag></a><a target="_blank" class="s15a223be" href="https://an.yandex.ru/count/6a6gmoUn6u450Eu1CKXPQri00000EAQB4K02I09Wl0Xe172exUw02e01m9oQWBowxEVj0OW1bgAFy32G0QYrhjmVc06adSxQ8A01fhMkt1-e0PATpjeWk07IeRZ85C01HjW1tAIE3-01jecD3kW1dW7u0G680WIW0ixKgHQv0fKmhek7oiWAy0AUokV02VW2We20W830Qe03sRtyiWk80ulNfFy7c0FqWWEm0mIu1Fy1w0I95_W4Zv8KY0MFaXIG1VM06A05k8K6g0MwZmMm1RgF1RW5e8q6m0NrW1Z81Q2D1j05gxS2u0Lby0K1c0Q0qApp3g06vAW1g0Rag06u1WR91heeCTXEN_K0qGQ6wv8Mcua66Ta60000K2e0002G1pQf1rSXcCWNYIC_i0U0W90ym0Vko8At2j070k07XWg020RG2BgAW870a802u0YdwfGBW0e1mGe00000003mFzWA0k0AW8bw-0h0_1M82mYg2n0GbU5_Lo400FA75CFoSWK0m0k0emN82u3Kam7P2rSXcCWNYIC_w0kFaXJm2mg83ERtthu1w0m6c0ssvbpo3G3w3G223W293W0000000F0_a0x0X3sO3hhHnC7jwTTwe0x0X3sm3W6X3m0000000F0_g0_Nml2ysQBYaJy0?stat-id=4&amp;test-tag=468396516835329&amp;format-type=0&amp;banner-test-tags=eyI0MjU3NTk4ODA0IjoiNDY4MzkxOTUzNDY1MzQ0In0%3D&amp;"><yatag class="ge9745d90 ba42669fe">Отзывы о курсе</yatag></a><a target="_blank" class="s15a223be" href="https://an.yandex.ru/count/6a6gmwVvTYq50Eu1CKXPQri00000EAQB4K02I09Wl0Xe172exUw02e01m9oQWBowxEVj0OW1bgAFy32G0QYrhjmVc06adSxQ8A01fhMkt1-e0PATpjeWk07IeRZ85C01LjW1tAIE3-01jecD3kW1dW7u0G680WIW0ixKgHQv0fKmhek7oiWAy0AUokV02VW2We20W830Qe03sRtyiWk80ulNfFy7c0FqWWEm0mIu1Fy1w0I95_W4Zv8KY0MFaXIG1VM06A05k8K6g0MwZmMm1RgF1RW5e8q6m0NrW1Z81Q2D1j05gxS2u0Lby0K1c0Q0qApp3g06vAW1g0Rag06u1WR91heeCTXEN_K0qGQ6wv8Mcua66Ta60000K2e0002G1pQf1rSXcCWNYIC_i0U0W90ym0Vko8At2j070k07XWg020RG2BgAW870a802u0YdwfGBW0e1mGe00000003mFzWA0k0AW8bw-0h0_1M82mYg2n0GbU5_Lo400FA75CFoSWK0m0k0emN82u3Kam7P2rSXcCWNYIC_w0kFaXJm2mg83ERtthu1w0m6c0ssvbpo3G3w3G223W293W0000000F0_a0x0X3sO3hhHnC7jwTTwe0x0X3sm3W6X3m0000000F0_g0_Nml2ysQBYaJy0?stat-id=4&amp;test-tag=468396516835329&amp;format-type=0&amp;banner-test-tags=eyI0MjU3NTk4ODA0IjoiNDY4MzkxOTUzNDY1MzQ0In0%3D&amp;"><yatag class="ge9745d90 ba42669fe">Трудоустройство</yatag></a></yatag></yatag></yatag></yatag></yatag><yatag class="t7ca33883"><yatag class="hcb776ed7"><a target="_blank" class="u1398411d g63a8eb0e" href="https://an.yandex.ru/count/6a6gmsiHl9a50Eu1CKXPQri00000EAQB4K02I09Wl0Xe172exUw02e01m9oQWBowxEVj0OW1bgAFy32G0QYrhjmVc06adSxQ8A01fhMkt1-e0PATpjeWk07IeRZ85C010jW1tAIE3-01jecD3kW1dW7u0G680WIW0ixKgHQv0fKmhek7oiWAy0AUokV02VW2We20W830Qe03sRtyiWk80ulNfFy7c0FqWWEm0mIu1Fy1w0I95_W4Zv8KY0MFaXIG1VM06A05k8K6g0MwZmMm1RgF1RW5e8q6m0NrW1Z81Q2D1j05gxS2u0Lby0K1c0Q0qApp3g06vAW1g0Rag06u1WR91heeCTXEN_K0qGQ6wv8Mcua66Ta60000K2e0002G1pQf1rSXcCWNYIC_i0U0W90ym0Vko8At2j070k07XWg020RG2BgAW870a802u0YdwfGBW0e1mGe00000003mFzWA0k0AW8bw-0h0_1M82mYg2n0GbU5_Lo400FA75CFoSWK0m0k0emN82u3Kam7P2rSXcCWNYIC_w0kFaXJm2mg83ERtthu1w0m6c0ssvbpo3G3w3G223W293W0000000F0_a0x0X3sO3hhHnC7jwTTwe0x0X3sm3W6X3m0000000F0_g0_Nml2ysQBYaJy0?stat-id=4&amp;test-tag=468396516835329&amp;format-type=0&amp;banner-test-tags=eyI0MjU3NTk4ODA0IjoiNDY4MzkxOTUzNDY1MzQ0In0%3D&amp;"><yatag class="fc4a8f30d">otus.ru</yatag><yatag class="x1637e89"><svg width="6" height="9"><path d="M1 1l3.65 3.65-3.547 3.547"></path></svg></yatag></a></yatag></yatag></yatag></yatag><yatag class="p356a4444 k2e97d717 qc5335706 h10792e45 q8d85868f"><yatag class="qf557301"><yatag class="b8262e991 e521709a0 p3a5ad383 p35595409 w9beccb99"><yatag class="sa7caaa36"><yatag class="w5f20b5d6" style="width: 8px !important; height: 8px !important;"><svg width="8px" height="8px" viewBox="0 0 14 14"><polygon points="14,0.7 13.3,0 7,6.299 0.7,0 0,0.7 6.3,7 0,13.299 0.7,14 7,7.7 13.3,14 14,13.299 7.7,7"></polygon></svg></yatag><yatag class="gf8afc984"><yatag class="oe841e490"></yatag><yatag class="u7a60a994"></yatag></yatag></yatag></yatag></yatag></yatag><yatag class="ld63d790f c473a75e2"><yatag class="d7123d458"><yatag class="rafc447b9"><yatag></yatag></yatag></yatag></yatag></yatag></td></tr></tbody></table></yatag></yatag><yatag class="ud667adfa k92ce93c9 f1e024653"><a target="_blank" class="c10b4f84e" href="https://direct.yandex.ru/?partner">Яндекс.Директ</a></yatag><yatag class="c620157b0"></yatag></yatag></yatag></div></div><div class="entry-content"><p>Ключевое
 слово null обычно используется во многих языках программирования, таких
 как Java, C++, C# и JavaScript. Это значение, которое присваивается 
переменной.</p><p>Концепция ключевого слова null в том, что она дает переменной нейтральное или "нулевое" поведение.</p><p>А что же в Python?</p><div class="section" id="null-python-none"><h2>Эквивалент null в Python: None</h2><p>Он был разработан таким образом, по двум причинам:</p><p>Многие утверждают, что слово <i>null</i> несколько эзотерично. Это не наиболее дружелюбное слово для новичков. Кроме того, <b>None</b> относится именно к требуемой функциональности - это ничего, и не имеет поведения.</p><p>Присвоить переменной значение None очень просто:</p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="n">my_none_variable</span> <span class="o">=</span> <span class="kc">None</span></p><p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; font-size:10px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p><p>Существует много случаев, когда следует использовать None.</p><p>Часто
 вы хотите выполнить действие, которое может работать либо завершиться 
неудачно. Используя None, вы можете проверить успех действия. Вот 
пример:</p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="c1"># Мы хотели бы подключиться к базе данных. Мы не знаем, верны ли логин и пароль</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="c1"># Если соединение с базой будет неуспешно, то</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="c1"># Он бросит исключение. Обратите внимание, что MyDatabase и DatabaseException</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="c1"># НЕ являются реальными классами, мы просто используем их в качестве примеров.</span></p><p style="-qt-paragraph-type:empty; margin-top:0px; background-color:#eff0f1; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="k">try</span><span class="p">:</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">    <span class="n">database</span> <span class="o">=</span> <span class="n">MyDatabase</span><span class="p">(</span><span class="n">db_host</span><span class="p">,</span> <span class="n">db_user</span><span class="p">,</span> <span class="n">db_password</span><span class="p">,</span> <span class="n">db_database</span><span class="p">)</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">    <span class="n">database_connection</span> <span class="o">=</span> <span class="n">database</span><span class="o">.</span><span class="n">connect</span><span class="p">()</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="k">except</span> <span class="n">DatabaseException</span><span class="p">:</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">    <span class="k">pass</span></p><p style="-qt-paragraph-type:empty; margin-top:0px; background-color:#eff0f1; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="k">if</span> <span class="n">database_connection</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">    <span class="nb">print</span><span class="p">(</span><span class="s1">'The database could not connect'</span><span class="p">)</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="k">else</span><span class="p">:</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">    <span class="nb">print</span><span class="p">(</span><span class="s1">'The database could connect'</span><span class="p">)</span></p><p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; font-size:10px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p><p>Python является объектно-ориентированным, и поэтому None - тоже объект, и имеет свой тип.</p><div class="highlight-python3" style="position: relative;"><div class="highlight"><span class="copybutton" title="Убрать приглашения и вывод программы">&gt;&gt;&gt;</span><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="nb"></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">type</span><span class="p">(</span><span class="bp">None</span><span class="p">)</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="go">&lt;class 'NoneType'&gt;</span></p><p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; font-size:10px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p></div></div></div><div class="section" id="none"><h2>Проверка на None</h2><p>Есть (формально) два способа проверить, на равенство None.</p><p>Один из способов - с помощью <a href="https://pythonworld.ru/osnovy/klyuchevye-slova-modul-keyword.html" target="_blank">ключевого слова</a> <b>is</b>.</p><p>Второй - с помощью <b>==</b> (но никогда не пользуйтесь вторым способом, и я попробую объяснить, почему).</p><p>Посмотрим на примеры:</p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="n">null_variable</span> <span class="o">=</span> <span class="kc">None</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="n">not_null_variable</span> <span class="o">=</span> <span class="s1">'Hello There!'</span></p><p style="-qt-paragraph-type:empty; margin-top:0px; background-color:#eff0f1; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="c1"># The is keyword</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="k">if</span> <span class="n">null_variable</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">    <span class="nb">print</span><span class="p">(</span><span class="s1">'null_variable is None'</span><span class="p">)</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="k">else</span><span class="p">:</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">    <span class="nb">print</span><span class="p">(</span><span class="s1">'null_variable is not None'</span><span class="p">)</span></p><p style="-qt-paragraph-type:empty; margin-top:0px; background-color:#eff0f1; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="k">if</span> <span class="n">not_null_variable</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">    <span class="nb">print</span><span class="p">(</span><span class="s1">'not_null_variable is None'</span><span class="p">)</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="k">else</span><span class="p">:</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">    <span class="nb">print</span><span class="p">(</span><span class="s1">'not_null_variable is not None'</span><span class="p">)</span></p><p style="-qt-paragraph-type:empty; margin-top:0px; background-color:#eff0f1; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p><p style="-qt-paragraph-type:empty; margin-top:0px; background-color:#eff0f1; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="c1"># The == operator</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="k">if</span> <span class="n">null_variable</span> <span class="o">==</span> <span class="kc">None</span><span class="p">:</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">    <span class="nb">print</span><span class="p">(</span><span class="s1">'null_variable is None'</span><span class="p">)</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="k">else</span><span class="p">:</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">    <span class="nb">print</span><span class="p">(</span><span class="s1">'null_variable is not None'</span><span class="p">)</span></p><p style="-qt-paragraph-type:empty; margin-top:0px; background-color:#eff0f1; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="k">if</span> <span class="n">not_null_variable</span> <span class="o">==</span> <span class="kc">None</span><span class="p">:</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">    <span class="nb">print</span><span class="p">(</span><span class="s1">'not_null_variable is None'</span><span class="p">)</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="k">else</span><span class="p">:</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">    <span class="nb">print</span><span class="p">(</span><span class="s1">'not_null_variable is not None'</span><span class="p">)</span></p><p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; font-size:10px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p><p>Данный код выведет:</p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="n">null_variable</span> <span class="ow">is</span> <span class="kc">None</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="n">not_null_variable</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="n">null_variable</span> <span class="ow">is</span> <span class="kc">None</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="n">not_null_variable</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span></p><p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; font-size:10px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p><p>Отлично,
 так они делают одно и то же! Однако, не совсем. Для встроенных типов - 
да. Но с пользовательскими классами вы должны быть осторожны. Python 
предоставляет возможность <a href="https://pythonworld.ru/osnovy/peregruzka-operatorov.html" target="_blank">переопределения</a> операторов сравнения в пользовательских классах. Таким образом, вы можете сравнить классы, например, MyObject == MyOtherObject.</p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="k">class</span> <span class="nc">MyClass</span><span class="p">:</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">    <span class="k">def</span> <span class="nf">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">my_object</span><span class="p">):</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">        <span class="c1"># Просто вернем True</span></p><p style="-qt-paragraph-type:empty; margin-top:0px; background-color:#eff0f1; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">        <span class="k">return</span> <span class="kc">True</span></p><p style="-qt-paragraph-type:empty; margin-top:0px; background-color:#eff0f1; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="n">my_class</span> <span class="o">=</span> <span class="n">MyClass</span><span class="p">()</span></p><p style="-qt-paragraph-type:empty; margin-top:0px; background-color:#eff0f1; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="k">if</span> <span class="n">my_class</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">    <span class="nb">print</span><span class="p">(</span><span class="s1">'my_class is None, using the is keyword'</span><span class="p">)</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="k">else</span><span class="p">:</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">    <span class="nb">print</span><span class="p">(</span><span class="s1">'my_class is not None, using the is keyword'</span><span class="p">)</span></p><p style="-qt-paragraph-type:empty; margin-top:0px; background-color:#eff0f1; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="k">if</span> <span class="n">my_class</span> <span class="o">==</span> <span class="kc">None</span><span class="p">:</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">    <span class="nb">print</span><span class="p">(</span><span class="s1">'my_class is None, using the == syntax'</span><span class="p">)</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="k">else</span><span class="p">:</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">    <span class="nb">print</span><span class="p">(</span><span class="s1">'my_class is not None, using the == syntax'</span><span class="p">)</span></p><p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; font-size:10px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p><p>И получаем немного неожиданный результат:</p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="n">my_class</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">,</span> <span class="n">using</span> <span class="n">the</span> <span class="ow">is</span> <span class="n">keyword</span></p><p style=" font-family:'Courier New,Courier,sans-serif'; background-color:#eff0f1; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span class="n">my_class</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">,</span> <span class="n">using</span> <span class="n">the</span> <span class="o">==</span> <span class="n">syntax</span></p><p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; font-size:10px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p><p>Интересно, не правда ли? Вот поэтому нужно проверять на None с помощью ключевого слова is.</p><p>А ещё (для некоторых классов) вызов метода __eq__ может занимать много времени, и is будет просто-напросто быстрее.</p></div></div>
</body>
</html></p></body></html>